﻿Imports System.Security.Cryptography
Imports System.Xml
Imports System.Globalization
Imports System.IO

Public Class frmLoginMain

    Private Sub do_MsgBox(ByVal str As String)
        MessageBox.Show(str, "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If btnLogin.Enabled = False Then Exit Sub
        btnLogin.Enabled = False
        'Cek apakah kita dapat terhubung ke server mysql
        If mysql_isconnected() Then
            Try
                Dim data As DataTable = mysql_query(String.Format("SELECT * FROM tb_user WHERE BINARY username='{0}' AND BINARY password=MD5('{1}') LIMIT 1", txtUser.Text, txtPass.Text))
                If data.Rows.Count > 0 Then
                    localNick = data.Rows(0)("nama_user")
                    localUID = data.Rows(0)("id_user")
                    If data.Rows(0)("level").Equals("0") Then frmMain.iManageUser.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
                    If data.Rows(0)("level").Equals("1") Then frmMain.iManageUser.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
                    txtUser.Clear()
                    txtPass.Clear()
                    txtUser.Focus()
                    Me.Hide()
                    frmMain.Show()
                Else
                    do_MsgBox("Login Gagal" & vbCrLf & "Reason: Invalid Username/Password")
                End If
            Catch ex As Exception
                do_MsgBox("Login Gagal" & vbCrLf & "Reason: Tidak dapat terhubung ke server Database" & vbCrLf & ex.Message)
            End Try

        Else
            do_MsgBox("Login Gagal" & vbCrLf & "Reason: Tidak dapat terhubung ke server Database")
        End If
        btnLogin.Enabled = True
    End Sub

    Private Sub txtUser_KeyDown(sender As Object, e As KeyEventArgs) Handles txtUser.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtPass.Focus()
        End If
    End Sub

    Private Sub txtPass_KeyDown(sender As Object, e As KeyEventArgs) Handles txtPass.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnLogin.PerformClick()
        End If
    End Sub

    Private Sub SimpleButtonServer_Click(sender As Object, e As EventArgs) Handles SimpleButtonServer.Click
        frmDBOption.ShowDialog()
    End Sub

    Private Sub frmLoginMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        notifyMaster.Visible = False
        notifyMaster.Dispose()
    End Sub

    Private Sub frmLoginMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        notifyMaster.ShowBalloonTip(300)

        If System.IO.File.Exists(My.Application.Info.DirectoryPath.ToString & "\client.xml") = False Then
            client_write_configuration()
        End If
        client_read_configuration()
        CreateNewKeys()

        frmMain.Show()
        If auto_login Then
            frmMain.doLogin(username, password)
        End If

    End Sub

    Public Sub setNotifyBaloon(ByVal title As String, ByVal messages As String, ByVal icon As System.Windows.Forms.ToolTipIcon)
        notifyMaster.BalloonTipIcon = icon
        notifyMaster.BalloonTipTitle = title
        notifyMaster.BalloonTipText = messages
        notifyMaster.ShowBalloonTip(300)
    End Sub

    Private Sub CreateNewKeys()
        Dim Keys As Keypair = Keypair.CreateNewKeys

        'Dim EncryptedMessage As RSAResult = cRSA.Encrypt("SAYA GANTENG", Keys.Publickey)
        'Dim DecryptedMessage As RSAResult = cRSA.Decrypt(EncryptedMessage.AsBytes, Keys.Privatekey)
        'Console.WriteLine("Encrypted RSA : " & EncryptedMessage.AsString)
        'Console.WriteLine("Decrypted RSA : " & DecryptedMessage.AsString)
        'Dim EncryptedString As String = AES_Encrypt("Enkripsi Saya", AES_SecretKey)
        'Console.WriteLine("Encrypted AES : " & EncryptedString)
        'Console.WriteLine("Decrypted AES : " & AES_Decrypt(EncryptedString, AES_SecretKey))
        'Console.WriteLine("HMAC Hash : " & getMessageHMACHash("Enkripsi Saya"))
    End Sub

    Private Sub ToolStripMenuItemExit_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItemExit.Click
        Me.Close()
    End Sub

    Private Sub ToolStripMenuItemSettings_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItemSettings.Click
        frmDBOption.ShowDialog()
    End Sub
End Class